package controller;

import lombok.Data;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import service.IUserService;
import tools.CustomStringUtil;
import tools.VerificationCodeBuilder;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

@Data
@RestController
public class LoginAction {

    @Resource(name = "userOperations")
    private IUserService userOperations;

    @Resource(name = "verificationCodeTool")
    private VerificationCodeBuilder verificationCode;

    @RequestMapping(value = "login_verification.action", method = RequestMethod.GET, produces = {"text/html;charset=UTF-8"})
    public String loginVerification(@RequestParam("user_name") String userName) {
        if (userOperations.getUsersCount(userName) == 1) {
            return "true";
        } else {
            return "false";
        }
    }

    @RequestMapping(value = "login_confirm.action", method = RequestMethod.GET, produces = {"text/html;charset=UTF-8"})
    public String loginConfirm(@RequestParam("user_name") String userName, @RequestParam("user_password") String userPassword, @RequestParam("captcha") String captcha, HttpSession session) {
        String matchingCaptcha = (String) session.getAttribute("validate_code");
        if (matchingCaptcha == null || !matchingCaptcha.equals(captcha)) {
            return "captcha_error";
        } else {
            String getPassword = userOperations.getPassword(userName);
            String getMD5Password = CustomStringUtil.getMD5String2(userPassword);
            if (getPassword != null && getPassword.equals(getMD5Password)) {
                session.setAttribute("login_confirm", "true");
                return "all_correct";
            } else {
                return "login_deny";
            }
        }
    }

    @RequestMapping(value = "/login_getVerificationCode.action", method = RequestMethod.GET)
    public void validateCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        verificationCode.generateCodeAndImage();
        request.getSession(true).setAttribute("validate_code", verificationCode.getResultCode());
        BufferedImage image = verificationCode.getResultImage();
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        OutputStream os = response.getOutputStream();
        ImageIO.write(image, "jpeg", os);
        os.flush();
    }

    @RequestMapping(value = "/match_captcha.action", method = RequestMethod.GET)
    public String matchingCaptcha(@RequestParam("captcha") String captcha, HttpSession session) {
        String matchingCaptcha = (String) session.getAttribute("validate_code");
        if (matchingCaptcha == null || !matchingCaptcha.equals(captcha)) {
            return "deny";
        } else {
            return "correct";
        }
    }
}
